﻿using System;
using System.Collections.Generic;
using T.FAS.Runtime.Action.SPI;
using T.FAS.Runtime.Base.Entity;
using T.FAS.Runtime.Excel.Dao;
using T.FAS.Runtime.Excel.Entity;
using T.FAS.Runtime.Excel.SPI;
using AppContext = T.ServiceComponent.Session.Service.AppContext;

namespace T.FAS.Runtime.Excel.Service
{
    public class ExportTaskManager
    {
        readonly ExportTaskLogic exportTaskLogic = new ExportTaskLogic();

        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="dataQueryID"></param>
        /// <param name="parameters"></param>
        /// <param name="exportDefs"></param>
        /// <param name="exportType"></param>
        public ExportTask ExportData(ExportParam exportParam, List<ExportParameter> parameters, List<ExportDef> exportDefs)
        {
            IExportExecutor exportExecutor = new ExportExecutor();
            return exportExecutor.ExportData(exportParam, parameters, exportDefs);
        }
        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="dataQueryID"></param>
        /// <param name="parameters"></param>
        /// <param name="exportDefs"></param>
        /// <param name="exportType"></param>
        public ExportTask ExportDataQuery(string dataQueryID, List<ExportParameter> parameters, List<ExportDef> exportDefs, int exportType, int type)
        {
            ExportParam exportParam = new ExportParam();
            exportParam.MetaDataID = dataQueryID;
            exportParam.ActionType = 3;
            exportParam.Type = type;
            exportParam.MaxRow = 1000000;
            exportParam.ExportType = exportType;
            IExportExecutor exportExecutor = new ExportExecutor();
            return exportExecutor.ExportData(exportParam, parameters, exportDefs);
        }
        /// <summary>
        /// 查询导出任务列表
        /// </summary>
        /// <param name="dataQueryID"></param>
        /// <param name="filter"></param>
        /// <returns></returns>
        public Dictionary<string, object> GetExportTaskList(string dataQueryID, Dictionary<string, string> filter)
        {
            return exportTaskLogic.GetTaskList(dataQueryID, filter);
        }
        /// <summary>
        /// 删除任务
        /// </summary>
        /// <param name="taskID"></param>
        /// <returns></returns>
        public Dictionary<string, string> DeleteExportTask(string taskID)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            try
            {
                ExportTask exportTask = exportTaskLogic.GetTask(taskID);
                if(exportTask == null)
                {
                    dic.Add("state", "1");
                    return dic;
                }
                exportTaskLogic.DeleteTask(taskID);
                List<Resource> resources = new List<Resource>();
                resources.Add(new Resource { RelativeUrl = exportTask.FileURL });
                FileUtil.DelResult(resources);//删除资源服务
                dic.Add("state", "1");
            }
            catch (Exception e)
            {
                dic.Add("state", "0");
                dic.Add("errmsg", e.Message);
            }
            return dic;
        }
        /// <summary>
        /// 清空任务
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, string> ClearExportTask(string metaDataID)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            try
            {
                List<ExportTask> exportTasks = exportTaskLogic.GetTaskList(metaDataID);
                exportTaskLogic.ClearTask(metaDataID);
                List<Resource> resources = new List<Resource>();
                foreach (var item in exportTasks)
                {
                    resources.Add(new Resource { RelativeUrl = item.FileURL });
                }
                FileUtil.DelResult(resources);
                dic.Add("state", "1");
            }
            catch (Exception e)
            {
                dic.Add("state", "0");
                dic.Add("errmsg", e.Message);
            }
            return dic;
        }
    }
}
